package com.duolebo.uteped_sdk.ota;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.duolebo.uteped_sdk.ota.jl.JieLiOta;
import com.yc.nadalsdk.ble.open.DeviceModeJX;
import com.yc.nadalsdk.log.LogUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class OemOtaManager {

    private static final String TAG = "OemOtaManager";

    public static final int OTA_STATE_UNKNOWN = 0;
    public static final int OTA_STATE_IDLE = 1;
    public static final int OTA_STATE_PREPARING = 2;
    public static final int OTA_STATE_PREPARED = 3;
    public static final int OTA_STATE_TRANSFERRING = 4;
    public static final int OTA_STATE_TRANSFERRED = 5;

    Map<Integer, IOemOta> oemOTAMap = new HashMap<>();

    @SuppressLint("StaticFieldLeak")
    private static OemOtaManager instance;
    private final Context mContext;

    public static OemOtaManager getInstance(Context context) {
        if (instance == null) {
            instance = new OemOtaManager(context);
        }
        return instance;
    }

    public static OemOtaManager shareInstance() {
        return instance;
    }

    public OemOtaManager(Context context) {
        this.mContext = context;
        ActsOta actsOta = new ActsOta(context);
        this.oemOTAMap.put(BaseOemOta.OTA_TYPE_ACTS, actsOta);

        JieLiOta jieLiOta = new JieLiOta(context);
        this.oemOTAMap.put(BaseOemOta.OTA_TYPE_JL, jieLiOta);
    }

    private IOemOta getOem() {
        return this.oemOTAMap.get(BaseOemOta.getOtaType());
    }

    public void prepare() {
        registerBroadcastReceiver();
        for (Integer key:oemOTAMap.keySet()) {
            Objects.requireNonNull(oemOTAMap.get(key)).oemPrepare();
        }
    }

    public void startOTA(String filePath, BleOtaListener listener) {
        getOem().oemStartOTA(filePath, listener);
    }
    public void cancelOTA() {
        getOem().oemCancelOTA();
    }

    public void release() {
        for (Integer key:oemOTAMap.keySet()) {
            Objects.requireNonNull(oemOTAMap.get(key)).oemRelease();
        }
        unregisterBroadcastReceiver();
    }

    private void onBluetoothOff() {
        for (Integer key:oemOTAMap.keySet()) {
            Objects.requireNonNull(oemOTAMap.get(key)).oemBluetoothOff();
        }
    }

    public void registerBroadcastReceiver() {
        LogUtils.i(TAG, "注册广播" + mReceiver);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        mContext.registerReceiver(mReceiver, intentFilter);
    }

    public void unregisterBroadcastReceiver() {
        LogUtils.i(TAG, "注销广播 " + mReceiver);
        try {
            mContext.unregisterReceiver(mReceiver);
        } catch (Exception e) {
            LogUtils.e(TAG, "注销广播 e=" + e);
        }
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            LogUtils.i(TAG, "action =" + action);
            if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
                LogUtils.i(TAG, "state =" + state);
                switch (state) {
                    case BluetoothAdapter.STATE_OFF:
                        onBluetoothOff();
                        break;
                    case BluetoothAdapter.STATE_TURNING_OFF:
                    case BluetoothAdapter.STATE_ON:
                    case BluetoothAdapter.STATE_TURNING_ON:
                        break;
                }
            }

        }
    };
}

